from itertools import zip_longest

"""
zip函数可以将多个可迭代对象打包成元组，返回一个可迭代对象
该对象中的每个元素都是元组，每个元组包含来自每个可迭代对象的元素，
如果各个迭代器的元素个数不一致，则返回列表长度与最短的对象相同
"""
numbers = [1, 2, 3, 4]
letters = ['a', 'b', 'c']
x_list = ['d', 'e']
zipped = zip(numbers, letters, x_list)
print(list(zipped))

# for item in zipped:
#     print(item)

# 按照最长的迭代器打包，不足的时候补默认值
long_zipped = zip_longest(numbers, letters, x_list, fillvalue='默认值')
print(list(long_zipped))

"""
map函数会对可迭代对象中的每个元素应用函数
"""
numbers = [1, 2, 3, 4, 5]
maped = map(lambda a: pow(a, 2), numbers)
print(list(maped))
maped = [x * x for x in numbers]
print(maped)

"""
filter对可迭代对象中的每个元素进行过滤
"""
numbers = [1, 2, 3, 4, 5]
filtered = filter(lambda a: a <= 3, numbers)
print(list(filtered))
# 上面的函数等于下面的列表推导式
filtered = [x for x in numbers if x <= 3]
print(filtered)

"""
all 判断可迭代对象中的所有元素是否都为真，如果是返回True，否则返回False
"""
numbers1 = [1, 2, 3, 4, 5]
numbers2 = [0, 1, 2, 3, 4, 5]
print(all(numbers1))
print(all(numbers2))
sign = all(num > 0 for num in numbers1)
print(sign)


"""
any 判断可迭代对象中的任意一个元素是否为真，如果是返回True，否则返回False
"""
print(any(numbers1))
print(any(numbers2))
